package com.hcinfos.gsoo.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String getCurrentTime() {
		Date date = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = fmt.format(date);
		return dateStr;
	}

	public static String getMMHH() {
		Date date = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
		String dateStr = fmt.format(date);
		return dateStr;
	}

	public static int compare_date(String date2) {

		String date1 = getMMHH();
		DateFormat df = new SimpleDateFormat("hh:mm");
		try {
			java.util.Date d1 = df.parse(date1);
			java.util.Date d2 = df.parse(date2);
			if (d1.getTime() > d2.getTime()) {

				return 1;
			} else if (d1.getTime() < d2.getTime()) {

				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;

	}

	public static int compare_date(String date1, String date2) {

		DateFormat df = new SimpleDateFormat("hh:mm");
		try {
			java.util.Date d1 = df.parse(date1);
			java.util.Date d2 = df.parse(date2);
			if (d1.getTime() > d2.getTime()) {

				return 1;
			} else if (d1.getTime() < d2.getTime()) {

				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;

	}

	public static String getCurrentTimes() {
		Date date = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		String dateStr = fmt.format(date);
		return dateStr;
	}

	public static int daysBetween(String date1, String date2)
			throws ParseException {
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date smdate = s.parse(date1);
		Date bdate = s.parse(date2);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	public static String formatTime(String str) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(str);
		return fmt.format(date);
	}

	public static String formatTimeHHMM(String str) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Date date = new Date(str);
		return fmt.format(date);
	}

	public static String getCreateAt(Date date) {
		Calendar c = Calendar.getInstance();
		if (c.get(Calendar.YEAR) - (date.getYear() + 1900) > 0) {
			int i = c.get(Calendar.YEAR) - date.getYear();
			return i + "年前";
		} else if (c.get(Calendar.MONTH) - date.getMonth() > 0) {
			int i = c.get(Calendar.MONTH) - date.getMonth();
			return i + "月前";
		} else if (c.get(Calendar.DAY_OF_MONTH) - date.getDate() > 0) {
			int i = c.get(Calendar.DAY_OF_MONTH) - date.getDate();
			return i + "天前";
		} else if (c.get(Calendar.HOUR_OF_DAY) - date.getHours() > 0) {
			int i = c.get(Calendar.HOUR_OF_DAY) - date.getHours();
			return i + "小时前";
		} else if (c.get(Calendar.MINUTE) - date.getMinutes() > 0) {
			int i = c.get(Calendar.MINUTE) - date.getMinutes();
			return i + "分钟前";
		} else {
			return "刚刚";
		}
	}

	/**
	 * 判断当前日期是星期几<br>
	 * <br>
	 * 
	 * @param pTime
	 *            修要判断的时间<br>
	 * @return dayForWeek 判断结果<br>
	 * @Exception 发生异常<br>
	 */
	public static int dayForWeek() {
		int dayForWeek = 0;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());

			if (c.get(Calendar.DAY_OF_WEEK) == 1) {
				dayForWeek = 7;
			} else {
				dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
			}
		} catch (Exception e) {

		}

		return dayForWeek;
	}

	public static int dayForWeek(String time) {
		int dayForWeek = 0;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.setTime(format.parse(time));

			if (c.get(Calendar.DAY_OF_WEEK) == 1) {
				dayForWeek = 7;
			} else {
				dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
			}
		} catch (Exception e) {

		}

		return dayForWeek;
	}
}
